<?php
require_once '../config/config.php';
require_once 'inc/session.php';

$menu = 4;

$sql = 'SELECT id, id_etablissement, id_edition, id_abonnement, nb_beneficiares, date_creation, etat FROM commande WHERE offre_essai = 0';
$req = mysql_query($sql) or die(mysql_error());

if (isset($_GET['maj']))
    $message_succes = 'Abonnement mise à jour avec succès.';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Interface Administration | L'essentiel du sup</title>
        <link rel="shortcut icon" href="images/favicon.ico" />
        <link rel="stylesheet" href="css/style.css" type="text/css" />
        <link rel="stylesheet" href="css/jquery/jquery-ui-1.9.2.custom.min.css" />
        <!--[if IE 9]>
            <link rel="stylesheet" media="screen" href="css/ie9.css"/>
        <![endif]-->
        <!--[if IE 8]>
            <link rel="stylesheet" media="screen" href="css/ie8.css"/>
        <![endif]-->
        <!--[if IE 7]>
            <link rel="stylesheet" media="screen" href="css/ie7.css"/>
        <![endif]-->
        <style type="text/css">
            .filter .date-filter {
                height: 30px;
            }

            .filter .status-filter {
                height: 30px;
            }

            .filter .status-filter label {
                margin: 2px 20px 0 1px; 
                vertical-align: middle
            }

            .tableoptions .ui-datepicker-trigger {
                margin: 1px 0 0 3px;
                padding: 0;
                vertical-align: top;
            }

            div.ui-datepicker{ font-size:11px; }

            .date-pick {
                border: 1px solid #CCCCCC;
                padding: 4px;
            }
        </style>
        <script type="text/javascript" src="js/plugins/jquery-1.8.3.js"></script>
        <script type="text/javascript" src="js/plugins/jquery.flot.min.js"></script>
        <script type="text/javascript" src="js/plugins/jquery.flot.resize.min.js"></script>
        <script type="text/javascript" src="js/plugins/jquery-ui-1.9.2.custom.min.js"></script>
        <script type="text/javascript" src="js/plugins/jquery.ui.datepicker-fr.js"></script>
        <script type="text/javascript" src="js/plugins/jquery.dataTables.min.js"></script>
        <script type="text/javascript" src="js/plugins/jquery.alerts.js"></script>
        <script type="text/javascript" src="js/custom/general.js"></script>
        <script type="text/javascript" src="js/custom/tables.js"></script>
        <script type="text/javascript">
            jQuery(document).ready(function() {
                jQuery(".date-pick").datepicker({
                    minDate: new Date(2012, 0, 1),
                    //maxDate: new Date(2013, 0, 0),
                    dateFormat: 'dd/mm/yy',
                    showButtonPanel: true,
                    changeMonth: true,
                    changeYear: true,
                    showOn: "button",
                    buttonImage: "images/icons/default/calendar.png",
                    buttonText: "Afficher calendrier"
                });

                jQuery(".date-pick").click(function() {
                    jQuery(this).datepicker('show');
                });

                var oTable = jQuery('#dyntable').dataTable({
                    "iDisplayLength": 10,
                    "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "Tous"]],
                    "sPaginationType": "full_numbers",
                    "oLanguage": {
                        "sUrl": "js/language/dataTables.french.txt"
                    },
                    "sPaginationType": "full_numbers",
                            "bStateSave": true,
                    "fnStateSave": function(oSettings, oData) {
                        localStorage.setItem('DataTables', JSON.stringify(oData));
                    },
                    "fnStateLoad": function(oSettings) {
                        return JSON.parse(localStorage.getItem('DataTables'));
                    },
                    "aoColumnDefs": [{
                            "bSortable": false, "aTargets": [0]
                        }]
                });

                jQuery('#btn_details').click(function() {
                    var num_checked = jQuery('tbody input[type=checkbox]:checked').length;
                    if (num_checked == 0) {
                        jAlert("Veuillez sélectionner une ligne.", 'Attention');
                        return false;
                    }
                    if (num_checked > 1) {
                        jAlert("Veuillez sélectionner une seule ligne.", 'Attention');
                        return false;
                    }
                    var ch = jQuery('#dyntable').find('tbody input[type=checkbox]');
                    ch.each(function() {
                        if (jQuery(this).is(':checked')) {
                            id = jQuery(this).val();
                            sel = true;
                        }
                    });

                    targetUrl = 'details_abonne.php?cmd=' + id;
                    window.location.href = targetUrl;
                });

                jQuery('#btn_modifier').click(function() {
                    var num_checked = jQuery('tbody input[type=checkbox]:checked').length;
                    if (num_checked == 0) {
                        jAlert("Veuillez sélectionner une ligne.", 'Attention');
                        return false;
                    }
                    if (num_checked > 1) {
                        jAlert("Veuillez sélectionner une seule ligne.", 'Attention');
                        return false;
                    }
                    var ch = jQuery('#dyntable').find('tbody input[type=checkbox]');
                    ch.each(function() {
                        if (jQuery(this).is(':checked')) {
                            id = jQuery(this).val();
                            sel = true;
                        }
                    });

                    targetUrl = 'modifier_abonne.php?cmd=' + id;
                    window.location.href = targetUrl;
                });

                jQuery("#btn_exporter").click(function() {
                    targetUrl = 'export/liste_abonnes.php';
                    window.location.href = targetUrl;
                });

                jQuery("#btn_filter").click(function() {
                    oTable.fnDraw();
                });

                jQuery("#btn_reset").click(function() {
                    jQuery("#start_date").val('');
                    jQuery("#end_date").val('');
                    jQuery('input:checkbox[name=statut_compte]:checked').each(function() {
                        jQuery(this).attr('checked', false);
                        //# rendre en mode unchecked --> enlever la classe CSS
                        jQuery(this).parent().removeClass('checked');
                    });
                });
            });

            //# overwrite the push function of the datatable object
            jQuery.fn.dataTableExt.afnFiltering.push(
                    function(oSettings, aData, iDataIndex) {

                        //# filtrage par statut du compte
                        var arr_status_compte = [];
                        var filtre_status_compte = jQuery('input:checkbox[name=statut_compte]:checked').length;
                        if (filtre_status_compte > 0) {
                            jQuery('input:checkbox[name=statut_compte]:checked').each(function() {
                                arr_status_compte.push(jQuery(this).val());
                            });

                            var stat_en_attente = false;
                            var stat_acitve = false;
                            var stat_desactive = false;

                            if (jQuery.inArray('0', arr_status_compte) > -1)
                                stat_en_attente = true;
                            if (jQuery.inArray('1', arr_status_compte) > -1)
                                stat_acitve = true;
                            if (jQuery.inArray('2', arr_status_compte) > -1)
                                stat_desactive = true;

                            var iStatutCol = 8;
                            var statut = aData[iStatutCol];

                            if ((statut == 'En cours') && !stat_en_attente)
                                return false;
                            if ((statut == 'Activé') && !stat_acitve)
                                return false;
                            if ((statut == 'Désactivé') && !stat_desactive)
                                return false;
                        }

                        //# filtrage par intervalle de date
                        //# num colonne date à filtrer
                        var iDateCol = 2;
                        var date_inf = null;
                        var date_sup = null;

                        var val = jQuery("#start_date").val();
                        if (val != '') {
                            var val = val.split('/');
                            date_inf = new Date(val[2], (val[1] - 1), val[0]);
                        }

                        var val2 = jQuery("#end_date").val();
                        if (val2 != '') {
                            var val2 = val2.split('/');
                            date_sup = new Date(val2[2], (val2[1] - 1), val2[0]);
                        }

                        val = aData[iDateCol].split(' ');
                        val = val[0].split('-');
                        date_column = new Date(val[2], (val[1] - 1), val[0]);

                        if (date_inf == null && date_sup == null)
                            return true;
                        else if (date_inf == null) {
                            if (date_sup.getTime() >= date_column.getTime())
                                return true;
                        }
                        else if (date_sup == null) {
                            if (date_inf.getTime() <= date_column.getTime())
                                return true;
                        }
                        else if (date_inf.getTime() <= date_column.getTime() && date_sup.getTime() >= date_column.getTime())
                            return true;
                        return false;
                    }
            );
        </script>
        <!--[if lt IE 9]>
                <script src="js/css3-mediaqueries.js"></script>
        <![endif]-->
    </head>
    <body class="loggedin">
        <?php include('inc/header.php') ?>
        <div class="mainwrapper">
            <div class="mainwrapperinner">
                <?php include('inc/menu.php') ?>
                <div class="maincontent noright">
                    <div class="maincontentinner">
                        <ul class="maintabmenu">
                            <li class="current">
                                <a>Gestion des abonnees</a>
                            </li>
                        </ul>
                        <div class="content">
                            <?php if (!empty($message_succes)) {
                                ?>
                                <div class="notification msgsuccess">
                                    <a class="close"></a>
                                    <p><?php echo $message_succes; ?></p>
                                </div>
                            <?php } ?>
                            <?php if (!empty($message_erreur)) {
                                ?>
                                <div class="notification msgerror">
                                    <a class="close"></a>
                                    <p><?php echo $message_erreur ?></p>
                                </div>
                            <?php } ?>
                            <div class="contenttitle radiusbottom0">
                                <h2 class="table">
                                    <span>Liste des abonnees</span>
                                </h2>
                            </div>
                            <div class="tableoptions filter">
                                <div class="date-filter">
                                    De : &nbsp; 
                                    <input type="text" class="date-pick" id="start_date" /> &nbsp; &nbsp; &nbsp;
                                    jusqu'à : &nbsp; 
                                    <input type="text" class="date-pick" id="end_date" />
                                </div>
                                <div class="status-filter">
                                    Statut :  &nbsp; &nbsp;
                                    <input name="statut_compte" value="0" type="checkbox" /> <label>En cours</label>
                                    <input name="statut_compte" value="1" type="checkbox" /> <label>Activé</label>
                                    <input name="statut_compte" value="2" type="checkbox" /> <label>Désactivé</label>
                                </div>
                                <div>
                                    <button class="radius3" id="btn_filter">Recherche</button>&nbsp;
                                    <button class="radius3" id="btn_reset">Annuler</button> 
                                </div>
                            </div>
                            <table cellpadding="0" cellspacing="0" border="0" class="stdtable stdtablecb" id="dyntable">
                                <colgroup>
                                    <col class="con0" />
                                    <col class="con1" />
                                    <col class="con0" />
                                    <col class="con1" />
                                    <col class="con0" />
                                    <col class="con1" />
                                    <col class="con0" />
                                    <col class="con1" />
                                    <col class="con0" />
                                    <col class="con1" />
                                    <col class="con0" />
                                    <col class="con1" />
                                    <col class="con0" />
                                    <col class="con1" />
                                </colgroup>
                                <thead>
                                    <tr>
                                        <th class="head0"><input type="checkbox" class="checkall" /></th>
                                        <th class="head1">N°</th>
                                        <th class="head0">Date de création</th>
                                        <th class="head1">Etablissement</th>
                                        <th class="head0">Ville</th>
                                        <th class="head1">Contact administratif</th>
                                        <th class="head0">Edition</th>
                                        <th class="head1">Nombre de bénéficiaires</th>
                                        <th class="head0">Statut</th>
                                        <th class="head1">Connu</th>
                                    </tr>
                                </thead>
                                <tfoot>
                                    <tr>
                                        <th class="head0"><input type="checkbox" class="checkall" /></th>
                                        <th class="head1">N°</th>
                                        <th class="head0">Date de création</th>
                                        <th class="head1">Etablissement</th>
                                        <th class="head0">Ville</th>
                                        <th class="head1">Contact administratif</th>
                                        <th class="head0">Edition</th>
                                        <th class="head1">Nombre de bénéficiaires</th>
                                        <th class="head0">Statut</th>
                                        <th class="head1">Connu</th>
                                    </tr>
                                </tfoot>
                                <tbody id="data">
                                    <?php
                                    while ($res = mysql_fetch_array($req)) {
                                        $id_commande = $res['id'];
                                        $date_creation = date('d-m-Y H:i', strtotime($res['date_creation']));

                                        //# etablissement
                                        $id_etablisssment = $res['id_etablissement'];
                                        $sql_etablissement = "SELECT etablissement, ville, connaissance FROM etablissement WHERE id = " . $id_etablisssment;
                                        $req_etablissement = mysql_query($sql_etablissement);
                                        $res_etablissement = mysql_fetch_array($req_etablissement);

                                        $etablissement = $res_etablissement['etablissement'];
                                        $ville = $res_etablissement['ville'];
                                        //# connaissance
                                        switch ($res_etablissement['connaissance']) {
                                            case 1:
                                                $connaissance = "Par un moteur de recherche";
                                                break;
                                            case 2:
                                                $connaissance = "Par un abonné";
                                                break;
                                            case 3:
                                                $connaissance = "Par les réseaux sociaux";
                                                break;
                                            case 4:
                                                $connaissance = "Par la presse";
                                                break;
                                            case 5:
                                                $connaissance = "Par un emailing";
                                                break;
                                            case 6:
                                                $connaissance = "Par un contact avec l’équipe de l’Essentiel du sup";
                                                break;
                                            case 7:
                                                $connaissance = "Autre";
                                                break;
                                            default :
                                                $connaissance = "-";
                                                break;
                                        }

                                        //# contact administrative
                                        $sql_contact = "SELECT nom, prenom, fonction FROM contact_administratif WHERE id_etablissement = " . $id_etablisssment;
                                        $req_contact = mysql_query($sql_contact);
                                        $res_contact = mysql_fetch_array($req_contact);

                                        $nom_contact = ucwords($res_contact['nom']) . ' ' . ucwords($res_contact['prenom']);
                                        $fonction_contact = $res_contact['fonction'];

                                        //# edition
                                        switch ($res['id_edition']) {
                                            case 1:
                                                $edition = "Ecoles de management";
                                                break;
                                            case 2:
                                                $edition = "Ecoles d’ingénieurs";
                                                break;
                                            case 3:
                                                $edition = "Universités";
                                                break;
                                        }

                                        //# nb beneficiares
                                        $sql_beneficiare = "SELECT id FROM beneficiare WHERE id_commande = " . $id_commande;
                                        $req_beneficiare = mysql_query($sql_beneficiare);
                                        $nb_beneficiares = mysql_num_rows($req_beneficiare);

                                        //# statut
                                        switch ($res['etat']) {
                                            case 0:
                                                $statut = "En cours";
                                                break;
                                            case 1:
                                                $statut = "Activé";
                                                break;
                                            case 2:
                                                $statut = "Désactivé";
                                                break;
                                            default:
                                                break;
                                        }
                                        ?>
                                        <tr class="gradeX" style="margin-bottom: 10px;">
                                            <td class="center">
                                                <input id="checkbox_<?php echo $id_commande ?>"  type="checkbox" value="<?php echo $id_commande ?>" />
                                            </td>
                                            <td align="center"><?php echo $id_commande ?></td>
                                            <td><?php echo $date_creation ?></td>
                                            <td><?php echo $etablissement ?></td>
                                            <td align="center"><?php echo $ville ?></td>
                                            <td><?php echo $nom_contact ?>, <?php echo $fonction_contact ?></td>
                                            <td><?php echo $edition ?></td>
                                            <td align="center"><?php echo $nb_beneficiares ?></td>
                                            <td align="center"><?php echo $statut ?></td>
                                            <td align="center"><?php echo $connaissance ?></td>
                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                            <div class="tableoptions2">
                                <button id="btn_details" class="radius3">Afficher</button> &nbsp;
                                <button id="btn_modifier" class="radius3">Modifier</button> &nbsp;
                                <button id="btn_exporter" class="radius3">Exporter en Excel</button> &nbsp;
                            </div>
                            <br clear="all" />
                        </div>
                    </div>
                    <div class="footer">
                        <p></p>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>
